import HttpUtils from '../HttpUtils/HttpUtils';
// 获取当前时间戳
var timestamp = Date.parse(new Date()) / 1000;
Page({
  ...HttpUtils.func,
  data: {
    //人员信息
    memberInfo: {},
    pageIndex: 1,
    pageItems: 10,
    totalPages: 0,
    status: '',

    //任务列表
    studyList: [],

    exam: '',
    meets: '',
    vote: '',
    //学习任务概况
    msgTipList: {},
    studyFactClass: '',
    examClass: '',
    meetsClass: '',
    voteClass: '',

    //未完成事项
    oldNoFinishList: [],
    noFinishList: [],
    indicatorDots: true,
    autoplay: false,
    showMiss: true,
    showExam: false,
    showMeet: false,
    showActiv: false,
    height: '',
  },

  missionPage(event) { //学习任务
    var pageUrl = "../mission/mission";
    wx.navigateTo({
      url: pageUrl
    })
  },
  examPage(event) { //考试
    var pageUrl = "../exam/exam";
    wx.navigateTo({
      url: pageUrl
    })
  },
  threeSessionse(event) { //跳转会议
    var pageUrl = "../threeSessions/threeSessions";
    wx.navigateTo({
      url: pageUrl
    })
  },
  votePage(event) { //活动
    var pageUrl = "../vote/vote";
    wx.navigateTo({
      url: pageUrl
    })
  },
  onLoad() {
    wx.getSystemInfo({ //设置手机屏幕宽度
      success: (res) => {
        console.log(res.screenWidth + "--" + res.screenHeight)
        this.setData({
          height: res.screenHeight / 2
        })
      }
    });

    var iconUrl = getApp().globalData.iconUrl;
    if (null == iconUrl || "" == iconUrl || undefined == iconUrl) {
      iconUrl = "../../img/headICON.png";
    }
    this.setData({
      "memberInfo.userName": getApp().globalData.userName,
      "memberInfo.partyName": getApp().globalData.partyName,
      "memberInfo.iconUrl": iconUrl
    });

    this.loadmsgTip(); //查询任务数量
    // this.loadStudyList(); //查询任务列表
    this.loadpartynoFinishList(); //查询未完成的任务 考试，会议，投票列表
  },


  //查询未完成的任务 考试，会议，投票列表
  loadpartynoFinishList() {
    var newUrl = getApp().globalData.dangjianUrl + "/study/studyList";
    var params = {};
    params["pageIndex"] = this.data.pageIndex + "";
    params["pageItems"] = this.data.pageItems + "";
    params["status"] = 2;
    params["studyStatus"] = 2;
    params = JSON.stringify(params);
    this.httpGet(newUrl, params, "查询任务列表接口调用失败").then(
      (respData) => {
        var result = respData.data.result.code;
        if (1 == result) {
          var studys = respData.data.studys;
          if (null != studys && undefined != studys) {
            studys = studys.study;
            for (var index in studys) {
              var record = studys[index];
              var status = record.status;
              var statusName = "";
              if (3 == status) {
                statusName = "已完成";
              } else if (2 == status) {
                statusName = "进行中";
              } else {
                statusName = "未开始";
              }
              record.statusName = statusName;
              record.menu = "学习";
            }
          }
          var noFinishList = [];
          noFinishList.push(studys);
          this.setData({
            "studyList": studys,
            "oldNoFinishList": noFinishList,
          });
          this.loadpartyExamList();
        } else {
          this.showPageMsg("alert", respData.data.result.description)
        }
      }
    );
  },

  //查询未完成的 我的考试任务列表
  loadpartyExamList: function(e) {
    var status = 1;
    var newUrl = getApp().globalData.dangjianUrl + "/exam/examList";
    var params = {};
    params["pageIndex"] = this.data.pageIndex + "";
    params["pageItems"] = this.data.pageItems + "";
    params["status"] = status + "";
    params = JSON.stringify(params);
    this.httpGet(newUrl, params, "查询我的考试任务列表接口调用失败").then(
      (respData) => {
        var result = respData.data.result.code;
        if (1 == result) {
          var exams = respData.data.data;
          if (null != exams && undefined != exams) {
            var examList = [];
            for (var index in exams.results) {
              var json = {};
              var exam = exams.results[index];
              json["taskId"] = exam.id;
              json["timeLimit"] = exam.timeLimit;
              json["endDate"] = exam.endTime;
              json["taskName"] = exam.paperName; //
              json["menu"] = "考试";
              var status;
              //获取开始时间戳
              var start = exam.startTime;
              start = start.substring(0, 19);
              start = start.replace(/-/g, '/');
              var getStartTime = new Date(start).getTime();
              //获取结束时间戳
              var end = exam.endTime;
              end = end.substring(0, 19);
              end = end.replace(/-/g, '/');
              var getEndTime = new Date(end).getTime();
              // 系统时间戳
              var sysTimeStamp = Date.parse(new Date())
              if (sysTimeStamp <= getStartTime) {
                status = 1; //未开始
              } else if (sysTimeStamp >= getStartTime && sysTimeStamp <= getEndTime) {
                status = 2; //进行中
              } else {
                status = 3; //已结束
              }
              json["status"] = status;
              //考试状态 != 结束
              if (status != 3) {
                examList.push(json);
              }
            }
          }
          var noFinishList = [];
          noFinishList.push(examList);
          var oldNoFinishList = this.data.oldNoFinishList;
          oldNoFinishList = oldNoFinishList.concat(noFinishList);
          this.setData({
            "noFinishList": oldNoFinishList,
            "oldNoFinishList": oldNoFinishList,
          });
          this.loadpartyMeetList();
        } else {
          this.showPageMsg("alert", respData.data.result.description)
        }
      }
    );
  },

  //查询我的 会议任务列表
  loadpartyMeetList: function(e) {
    var status = "";
    var newUrl = getApp().globalData.dangjianUrl + "/party/partyMeetList";
    var params = {};
    params["pageIndex"] = this.data.pageIndex + "";
    params["pageItems"] = this.data.pageItems + "";
    params["status"] = status + "";
    params["meetType"] = "";
    params["test"] = "";
    params = JSON.stringify(params);
    this.httpGet(newUrl, params, "支部会议查询列表接口调用失败").then(
      (respData) => {
        var result = respData.data.result.code;
        if (1 == result) {
          var meets = respData.data.meets;
          meets = meets.meet;
          var partyMeetList = [];
          for (var index in meets) {
            var meet = meets[index];
            var json = {};
            json["taskId"] = meet.meetId;
            json["endDate"] = this.formatDate("yyyy-MM-dd", meet.endDate);
            json["taskName"] = meet.title;
            json["sign"] = meet.sign;
            json["menu"] = "会议";
            if (meet.status != 3 && meet.sign == 0) {
              partyMeetList.push(json);
            }
          }
          var noFinishList = [];
          noFinishList.push(partyMeetList);
          var oldNoFinishList = this.data.oldNoFinishList;
          oldNoFinishList = oldNoFinishList.concat(noFinishList);
          this.setData({
            "noFinishList": oldNoFinishList,
            "oldNoFinishList": oldNoFinishList,
          });
          this.loadpartyVoteList();
        } else {
          this.showPageMsg("alert", respData.data.result.description)
        }
      }
    );
  },


  //投票列表查询接口
  loadpartyVoteList: function(e) {
    var newUrl = getApp().globalData.dangjianUrl + "/vote/partyVoteList";
    var params = {};
    params["pageIndex"] = this.data.pageIndex + "";
    params["pageItems"] = this.data.pageItems + "";
    params = JSON.stringify(params);
    this.httpGet(newUrl, params, "投票列表查询接口调用失败").then(
      (respData) => {
        console.log(respData)
        var result = respData.data.result.code;
        if (1 == result) {
          var votes = respData.data.vote;
          votes = votes.voteResponseList;
          var partyVoteList = [];
          for (var index in votes) {
            var vote = votes[index];
            var json = {};
            json["taskId"] = vote.voteId;
            json["status"] = vote.status;
            json["voteStatus"] = vote.voteUsers[0].voteStatus;
            json["menu"] = "投票";
            json["endDate"] = this.formatDate('yyyy-MM-dd', vote.endDate);
            json["taskName"] = vote.title;
            if (vote.status != 3 && vote.voteUsers[0].voteStatus == 0) {
              partyVoteList.push(json);
            }
          }
          var noFinishList = [];
          noFinishList.push(partyVoteList);
          var oldNoFinishList = this.data.oldNoFinishList;
          oldNoFinishList = oldNoFinishList.concat(noFinishList);
          this.setData({
            "noFinishList": oldNoFinishList,
            "oldNoFinishList": oldNoFinishList,
          });
        } else {
          this.showPageMsg("alert", respData.data.result.description)
        }
      }
    );
  },


  //查询任务概况
  loadmsgTip: function() {
    var newUrl = getApp().globalData.dangjianUrl + "/member/msgTip";
    var params = {};
    params["pageIndex"] = "1";
    params["pageItems"] = "10";
    params["status"] = 2;
    params = JSON.stringify(params);
    this.httpGet(newUrl, params, "党教中心消息况接口调用失败").then(
      (respData) => {
        var result = respData.data.result.code;
        if (1 == result) {
          var tip = respData.data.tip;
          var totalCount = 0;
          totalCount += parseInt(tip["learned"]);
          totalCount += parseInt(tip["learning"]);
          totalCount += parseInt(tip["unlearned"]);
          tip.totalCount = totalCount;
          var studyFactClass = tip.totalCount >= 99 ? 'num-big' : 'num-small';
          var examClass = tip.examCount >= 99 ? 'num-big' : 'num-small';
          var meetsClass = tip.meetCount >= 99 ? 'num-big' : 'num-small';
          var voteClass = tip.activityCount >= 99 ? 'num-big' : 'num-small';
          this.setData({
            "msgTipList": tip,
            "studyFactClass": studyFactClass,
            "examClass": examClass,
            "meetsClass": meetsClass,
            "voteClass": voteClass,
          });
        } else {
          this.showPageMsg("alert", respData.data.result.description)
        }
      }
    );
  },



  //功能暂未开发
  showUnDevMsg: function() {
    this.showPageMsg("tips", "功能暂未开发,敬请期待");
  },

  //触底
  onPullDownRefresh: function() {
    this.onLoad();
    wx.stopPullDownRefresh();
  },
  //跳转详情
  articleDetail(event) {
    var title = event.currentTarget.dataset.title;
    var detailCode = event.currentTarget.dataset.detailcode;
    var menu = event.currentTarget.dataset.menu;
    var status = event.currentTarget.dataset.status;
    var url;
    if (menu == "学习") {
      url = "../missionList/missionList?fromPage=task&taskId=" + detailCode + '&title=' + title;
    } else if (menu == "考试") {
      if (status == 1) {
        this.showPageMsg("tips", "考试暂未开始，请重新选择！");
      } else {
        var timeLimit = event.currentTarget.dataset.timelimit;
        url = '../examination/examination?examId=' + detailCode + '&timeLimit=' + timeLimit
      }
    } else if (menu == "会议") {
      var status = event.target.dataset.status;
      var sign = event.target.dataset.sign;
      url = '../meetingDetail/meetingDetail?meetId=' + detailCode + '&status=' + status + '&sign=' + sign;
    } else {
      var status = event.currentTarget.dataset.status;
      if (status == 1) {
        this.showPageMsg("tips", "投票暂未开始，敬请期待！");
      } else {
        url = '../voteDetail/voteDetail?voteId=' + detailCode
      }
    }
    if (url != '' && url != null) {
      wx.navigateTo({
        url: url,
      })
    }

  },
  handleTap(evnet) {
    var current = evnet.detail.current;
    if (current == 1) {
      this.setData({
        showMiss: false,
        showExam: true,
        showMeet: false,
        showActiv: false,
      })
    } else if (current == 2) {
      this.setData({
        showMiss: false,
        showExam: false,
        showMeet: true,
        showActiv: false,
      })
    } else if (current == 3) {
      this.setData({
        showMiss: false,
        showExam: false,
        showMeet: false,
        showActiv: true,
      })
    } else {
      this.setData({
        showMiss: true,
        showExam: false,
        showMeet: false,
        showActiv: false,
      })
    }
  },

});